﻿using System.Linq.Expressions;
using Hvam.SharePoint.Search.Language;
using Hvam.SharePoint.Search.Language.Visitors;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Hvam.SharePoint.Search.Tests.Internal.Language.Visitors
{
    [TestClass]
    public class ExpressionVisitorTests
    {
        [TestMethod]
        public void TestExpressionVisitorVisit()
        {
            // Arrange
            var expression =
                Expression.Equal(
                    KeywordQueryExpression.Property("id"),
                    Expression.Constant("1"));
            var visitor = new TestExpressionVisitor();
            TestExpressionVisitor.VisitedBinary = false;

            // Act
            visitor.VisitTest(expression);

            // Assert
            Assert.IsTrue(TestExpressionVisitor.VisitedBinary);
        }

        internal class TestExpressionVisitor : ExpressionVisitor
        {
            public static bool VisitedBinary { get; set; }

            public void VisitTest(Expression e)
            {
                Visit(e);
            }

            protected override Expression VisitBinary(BinaryExpression b)
            {
                VisitedBinary = true;
                return base.VisitBinary(b);
            }
        }
    }
}
